﻿using LJ.Abstractions.Exceptions;
using LJ.Abstractions.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;

namespace LJ.WebApi.UnifyResult.Mvc.Filters;


/// <summary>
/// 规范化结构（请求异常）过滤器
/// </summary>
public class UnifyExceptionFilter : IAsyncExceptionFilter, IOrderedFilter
{
    /// <summary>
    /// 排序属性
    /// </summary>
    public int Order => 8888;


    public Task OnExceptionAsync(ExceptionContext context)
    {
        if (context.ExceptionHandled == false)
        {
            var result = new WebApiResult<object>();

            var logger = context.HttpContext.RequestServices.GetLogger<UnifyExceptionFilter>();
            if (context.Exception is FriendlyException friendlyException)
            {
                logger.LogInformation("【规范化结构（请求异常）】: \r\n {Message}", friendlyException);
                result.Succeeded=false;
                result.StatusCode = friendlyException.StatusCode;
                result.Message= friendlyException.Message;
            }
            else
            {
                logger.LogError("【规范化结构（请求异常）】: \r\n {Message}", context.Exception);
                result.Succeeded = false;
                result.StatusCode = StatusCodes.Status500InternalServerError;
                result.Message = context.Exception.Message;
            }
            context.Result = new ObjectResult(result);
            context.HttpContext.Response.StatusCode = StatusCodes.Status200OK;
            context.ExceptionHandled = true;
        }
        return Task.CompletedTask;
    }
}
